package mashibing.class12;

import java.util.ArrayList;

/**
 * 搜索二叉树（根节点比左数最大值大，根节点比右数最小值小，左子树是搜索二叉树，右子树是搜索二叉树）
 *
 * @author yujiafan
 * @version 1.0.0
 * @create 2023-05-18
 */
public class Class12_2_IsBST {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static class Info {
        public boolean BST;
        public int min;
        public int max;

        public Info(boolean BST, int min, int max) {
            this.BST = BST;
            this.min = min;
            this.max = max;
        }
    }

    public static boolean isBST1(Node root) {
        if (root == null) {
            return true;
        }
        return process1(root).BST;
    }

    public static Info process1(Node node) {
        if (node == null) {
            return null;
        }
        Info leftInfo = process1(node.left);
        Info rightInfo = process1(node.right);
        // 左子树是搜索二叉树，右子树是搜索二叉树,根节点比左数最大值大，根节点比右数最小值小
        boolean BST = true;
        if ((leftInfo != null && !leftInfo.BST) || (rightInfo != null && !rightInfo.BST)
                || (leftInfo != null && node.value <= leftInfo.max)
                || (rightInfo != null && node.value >= rightInfo.min)) {
            BST = false;
        }
        int min = node.value;
        int max = node.value;
        if (leftInfo != null) {
            min = Math.min(min, leftInfo.min);
            max = Math.max(max, leftInfo.max);
        }
        if (rightInfo != null) {
            min = Math.min(min, rightInfo.min);
            max = Math.max(max, rightInfo.max);
        }
        return new Info(BST, min, max);
    }

    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (isBST1(head) != isBST2(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }

    public static boolean isBST2(Node head) {
        if (head == null) {
            return true;
        }
        ArrayList<Node> arr = new ArrayList<>();
        in(head, arr);
        for (int i = 1; i < arr.size(); i++) {
            if (arr.get(i).value <= arr.get(i - 1).value) {
                return false;
            }
        }
        return true;
    }

    public static void in(Node head, ArrayList<Node> arr) {
        if (head == null) {
            return;
        }
        in(head.left, arr);
        arr.add(head);
        in(head.right, arr);
    }
}
